//
//  UserPageSub2.swift
//  newsApp
//
//  Created by zhiyuwu on 2020/5/14.
//  Copyright © 2020 Doo Psyist. All rights reserved.
//

import SwiftUI
import MapKit
import UIKit
import CoreLocation

struct UserPageSub2: UIViewRepresentable {
        var newsListItem: NewsListItem
        
        func makeUIView(context: Context) -> MKMapView {
            MKMapView(frame: .zero)
            
            
            
            var posi = networkJson[0].pos
                    
            var pos = posi.components(separatedBy: ",")
                    
            var lati = (pos[0] as NSString).doubleValue
            
            var long = (pos[1] as NSString).doubleValue
            
            let mapView = MKMapView()
            
            
            let objectAnnotation = MKPointAnnotation()
            objectAnnotation.coordinate = CLLocation(latitude: lati, longitude: long).coordinate
            objectAnnotation.title = networkJson[0].title
            objectAnnotation.subtitle = networkJson[0].summary
            
            
            posi = networkJson[1].pos
            pos = posi.components(separatedBy: ",")
            lati = (pos[0] as NSString).doubleValue
            long = (pos[1] as NSString).doubleValue
            let objectAnnotation2 = MKPointAnnotation()
            objectAnnotation2.coordinate = CLLocation(latitude: lati, longitude: long).coordinate
            objectAnnotation2.title = networkJson[1].title
            objectAnnotation2.subtitle = networkJson[1].summary
            
            posi = networkJson[2].pos
            pos = posi.components(separatedBy: ",")
            lati = (pos[0] as NSString).doubleValue
            long = (pos[1] as NSString).doubleValue
            let objectAnnotation3 = MKPointAnnotation()
            objectAnnotation3.coordinate = CLLocation(latitude: lati, longitude: long).coordinate
            objectAnnotation3.title = networkJson[2].title
            objectAnnotation3.subtitle = networkJson[2].summary
            
            posi = networkJson[3].pos
            pos = posi.components(separatedBy: ",")
            lati = (pos[0] as NSString).doubleValue
            long = (pos[1] as NSString).doubleValue
            let objectAnnotation4 = MKPointAnnotation()
            objectAnnotation4.coordinate = CLLocation(latitude: lati, longitude: long).coordinate
            objectAnnotation4.title = networkJson[3].title
            objectAnnotation4.subtitle = networkJson[3].summary
            
            posi = networkJson[4].pos
            pos = posi.components(separatedBy: ",")
            lati = (pos[0] as NSString).doubleValue
            long = (pos[1] as NSString).doubleValue
            let objectAnnotation5 = MKPointAnnotation()
            objectAnnotation5.coordinate = CLLocation(latitude: lati, longitude: long).coordinate
            objectAnnotation5.title = networkJson[4].title
            objectAnnotation5.subtitle = networkJson[4].summary
            
            mapView.addAnnotation(objectAnnotation)
            mapView.addAnnotation(objectAnnotation2)
            mapView.addAnnotation(objectAnnotation3)
            mapView.addAnnotation(objectAnnotation4)
            mapView.addAnnotation(objectAnnotation5)
            
            return mapView
        }
        
        func updateUIView(_ uiView: MKMapView, context: Context) {
            
            let posi = newsListItem.pos
                    
            let pos = posi.components(separatedBy: ",")
                    
            //        let lati = pos[0]
            //        let long = pos[1]
                    
            let lati = (pos[0] as NSString).doubleValue
            
            let long = (pos[1] as NSString).doubleValue
            
            let coordinate = CLLocationCoordinate2D(
                latitude: lati, longitude: long)
            let span = MKCoordinateSpan(latitudeDelta: 0.015, longitudeDelta: 0.015)
    //        let region = MKCoordinateRegion(center: coordinate, span: span)
            
            
            uiView.showsUserLocation = true
            
            let region = MKCoordinateRegion(center: uiView.userLocation.location?.coordinate ?? coordinate, span: span)
            
            uiView.setRegion(region, animated: true)
            
            
           

           
        }
    }

struct UserPageSub2_Previews: PreviewProvider {
    static var previews: some View {
        UserPageSub2(newsListItem: newsList[0])
    }
}
